<?php

interface Middleware
{
    public static function handle(Closure $closure);
}

class VerifyCsrfToken implements Middleware
{
    public static function handle(Closure $next)
    {
        echo "验证 csrf token \r\n";
        $next();
    }
}

class VerifyAuth implements Middleware
{
    public static function handle(Closure $closure)
    {
        echo "验证是否登陆 \r\n";
        $closure();
    }
}

class SetCookie implements Middleware
{
    public static function handle(Closure $closure)
    {
        $closure();
        echo '设置cookie信息';
    }
}

$handle = function (){
  echo "当前要执行的程序";
};

$pipeArr = [
    'VerifyCsrfToken',
    'VerifyAuth',
    'SetCookie',
];

$callBack = array_reduce($pipeArr,function ($stack,$pipe){
   return function () use($stack,$pipe){
       return $pipe::handle($stack);
   };
},$handle);

call_user_func($callBack);
//function call_middleware() {
//    SetCookie::handle(function (){
//       VerifyAuth::handle(function(){
//           $handle = function (){
//               echo "当前要执行的程序！";
//           };
//           VerifyCsrfToken::handle($handle);
//       });
//    });
//}
//
//call_middleware();